<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.10"/>
<title>Introduction_to_Algorithms: src/graph_algorithms/max_flow/relabel_to_front/relabeltofront.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Introduction_to_Algorithms
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.10 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('relabeltofront_8h_source.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">relabeltofront.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="relabeltofront_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">/*</span></div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment"> * Copyright 2016- huaxz &lt;huaxz1986@163.com&gt;</span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment"> * This program is free software: you can redistribute it and/or modify</span></div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment"> * it under the terms of the GNU General Public License as published by</span></div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment"> * the Free Software Foundation, either version 2 of the License, or</span></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="comment"> * (at your option) any later version.</span></div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="comment"> * This program is distributed in the hope that it will be useful,</span></div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span></div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span></div>
<div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="comment"> * GNU General Public License for more details.</span></div>
<div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="comment"> * You should have received a copy of the GNU General Public License</span></div>
<div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="comment"> * along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span></div>
<div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;<span class="comment"> * Author: huaxz1986@163.com (huaxz)</span></div>
<div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;<span class="preprocessor">#ifndef RELABELTOFRONT</span></div>
<div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;<span class="preprocessor">#define RELABELTOFRONT</span></div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="preprocessor">#include&lt;memory&gt;</span></div>
<div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;<span class="preprocessor">#include&quot;../generic_push_relabel/genericpushrelabel.h&quot;</span></div>
<div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;<span class="preprocessor">#include&quot;../../basic_graph/graph_representation/graph_vertex/front_flow_vertex.h&quot;</span></div>
<div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;</div>
<div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespace_introduction_to_algorithm.html">IntroductionToAlgorithm</a></div>
<div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;{</div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;    <span class="keyword">namespace </span>GraphAlgorithm</div>
<div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;    {</div>
<div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;</div>
<div class="line"><a name="l00053"></a><span class="lineno"><a class="line" href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#a2ae42c12c93664d94b5a6f9980fe8540">   53</a></span>&#160;        <span class="keyword">template</span>&lt;<span class="keyword">typename</span> GraphType&gt; <span class="keywordtype">void</span> <a class="code" href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#a2ae42c12c93664d94b5a6f9980fe8540">discharge</a>(std::shared_ptr&lt;GraphType&gt; graph,<span class="keyword">typename</span> GraphType::VIDType u_id,</div>
<div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;                                               std::array&lt;std::array&lt;typename GraphType::EWeightType,GraphType::NUM&gt;,GraphType::NUM&gt;&amp;flow)</div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;        {</div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;</div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;            <span class="keyword">typedef</span> <span class="keyword">typename</span> GraphType::EWeightType EWeightType;</div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;            <span class="keywordflow">if</span>(!graph)</div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;discharge error: graph must not be nullptr!&quot;</span>);</div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;            <span class="keywordflow">if</span>(u_id&lt;0||u_id&gt;=GraphType::NUM)</div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;discharge error:id must &gt;=0 and &lt;N.&quot;</span>);</div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;            <span class="keyword">auto</span> vertex_u=graph-&gt;vertexes.at(u_id);</div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;            <span class="keywordflow">if</span>(!vertex_u)</div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;discharge error: vertex of id does not exist.&quot;</span>);</div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;            <span class="comment">//**************  开始循环  *******************</span></div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;            <span class="keywordflow">while</span>(vertex_u-&gt;key&gt;0)</div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;            {</div>
<div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;                 <span class="keyword">auto</span> node_v=vertex_u-&gt;N_List.current; <span class="comment">//</span></div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;                 <span class="keywordflow">if</span>(!node_v)</div>
<div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;                 {</div>
<div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;                     <a class="code" href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#a143924362cc9f23ca452c3ca0e292ff3">relabel</a>(graph,u_id,flow);</div>
<div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;                     vertex_u-&gt;N_List.current=vertex_u-&gt;N_List.head;</div>
<div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;                 }<span class="keywordflow">else</span></div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;                 {</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;                     EWeightType c_f=0;</div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;                     <span class="keyword">auto</span> vertex_v=node_v-&gt;value.lock();</div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;                     <span class="comment">//***********  获取 c_f(u,v)  **************</span></div>
<div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;                     assert(vertex_v);</div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;                     <span class="keyword">auto</span> v_id=vertex_v-&gt;id;</div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;                     <span class="keywordflow">if</span>(graph-&gt;has_edge(u_id,v_id))</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;                         c_f=graph-&gt;weight(u_id,v_id)-flow[u_id][v_id];</div>
<div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;                     <span class="keywordflow">else</span> <span class="keywordflow">if</span>(graph-&gt;has_edge(v_id,u_id))</div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;                         c_f=flow[v_id][u_id];</div>
<div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;                     <span class="keywordflow">else</span> <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;discharge error: (u,v) belongs E or (v,u) belongs E must be true!&quot;</span>);</div>
<div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;</div>
<div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;                     <span class="comment">//************  根据 c_f(u,v)以及 h函数决定是否 push  **************</span></div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;                     <span class="keywordflow">if</span>(c_f&gt;0 &amp;&amp;(vertex_u-&gt;h==vertex_v-&gt;h+1))</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;                         <a class="code" href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#abd520b7e33d8c1e72b05210d784ac258">push</a>(graph,u_id,v_id,flow);</div>
<div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;                     <span class="keywordflow">else</span></div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;                         vertex_u-&gt;N_List.current=vertex_u-&gt;N_List.current-&gt;next;</div>
<div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;</div>
<div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;                 }</div>
<div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;</div>
<div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;            }</div>
<div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160;        }</div>
<div class="line"><a name="l00098"></a><span class="lineno">   98</span>&#160;</div>
<div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;        <span class="keyword">template</span>&lt;<span class="keyword">typename</span> GraphType&gt; List&lt;ListNode&lt;typename GraphType::VertexType&gt;&gt;</div>
<div class="line"><a name="l00113"></a><span class="lineno"><a class="line" href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#ad6a1917551c87991625d8402593ec863">  113</a></span>&#160;        <a class="code" href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#ad6a1917551c87991625d8402593ec863">create_L</a> (std::shared_ptr&lt;GraphType&gt; graph,<span class="keyword">typename</span> GraphType::VIDType src,<span class="keyword">typename</span> GraphType::VIDType dst)</div>
<div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;        {</div>
<div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;            <span class="keyword">typedef</span> <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_list_node.html">ListNode&lt;typename GraphType::VertexType&gt;</a> LNodeType;</div>
<div class="line"><a name="l00116"></a><span class="lineno">  116</span>&#160;            <span class="keyword">typedef</span> <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_list.html">List&lt;LNodeType&gt;</a> LType;</div>
<div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;</div>
<div class="line"><a name="l00118"></a><span class="lineno">  118</span>&#160;            <span class="keywordflow">if</span>(!graph)</div>
<div class="line"><a name="l00119"></a><span class="lineno">  119</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;create_L error: graph must not be nullptr!&quot;</span>);</div>
<div class="line"><a name="l00120"></a><span class="lineno">  120</span>&#160;            <span class="keywordflow">if</span>(src&lt;0||src&gt;=GraphType::NUM||dst&lt;0||dst&gt;=GraphType::NUM)</div>
<div class="line"><a name="l00121"></a><span class="lineno">  121</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;create_L error:id must &gt;=0 and &lt;N.&quot;</span>);</div>
<div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;            <span class="keywordflow">if</span>(!graph-&gt;vertexes.at(src) || !graph-&gt;vertexes.at(dst))</div>
<div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;create_L error: vertex of id does not exist.&quot;</span>);</div>
<div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;</div>
<div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160;            LType L;</div>
<div class="line"><a name="l00126"></a><span class="lineno">  126</span>&#160;            <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i&lt;GraphType::NUM;i++)</div>
<div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;            {</div>
<div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;                <span class="keywordflow">if</span>(i==src||i==dst) <span class="keywordflow">continue</span>;</div>
<div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;                std::shared_ptr&lt;LNodeType&gt; node=std::make_shared&lt;LNodeType&gt;();</div>
<div class="line"><a name="l00130"></a><span class="lineno">  130</span>&#160;                node-&gt;value=graph-&gt;vertexes[i];</div>
<div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;                L.add(node);</div>
<div class="line"><a name="l00132"></a><span class="lineno">  132</span>&#160;            }</div>
<div class="line"><a name="l00133"></a><span class="lineno">  133</span>&#160;            <span class="keywordflow">return</span> L;</div>
<div class="line"><a name="l00134"></a><span class="lineno">  134</span>&#160;        }</div>
<div class="line"><a name="l00135"></a><span class="lineno">  135</span>&#160;</div>
<div class="line"><a name="l00136"></a><span class="lineno">  136</span>&#160;</div>
<div class="line"><a name="l00138"></a><span class="lineno">  138</span>&#160;</div>
<div class="line"><a name="l00152"></a><span class="lineno">  152</span>&#160;        <span class="keyword">template</span>&lt;<span class="keyword">typename</span> GraphType&gt; <span class="keywordtype">void</span></div>
<div class="line"><a name="l00153"></a><span class="lineno"><a class="line" href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#af1dfc9c1874850fb1415db3644497777">  153</a></span>&#160;        <a class="code" href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#af1dfc9c1874850fb1415db3644497777">initial_vertex_NList</a>(std::shared_ptr&lt;GraphType&gt; graph,<span class="keyword">typename</span> GraphType::VIDType src,<span class="keyword">typename</span> GraphType::VIDType dst)</div>
<div class="line"><a name="l00154"></a><span class="lineno">  154</span>&#160;        {</div>
<div class="line"><a name="l00155"></a><span class="lineno">  155</span>&#160;            <span class="keyword">typedef</span> <a class="code" href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_list_node.html">ListNode&lt;typename GraphType::VertexType&gt;</a> VertexNListNodeType;</div>
<div class="line"><a name="l00156"></a><span class="lineno">  156</span>&#160;</div>
<div class="line"><a name="l00157"></a><span class="lineno">  157</span>&#160;            <span class="keywordflow">if</span>(!graph)</div>
<div class="line"><a name="l00158"></a><span class="lineno">  158</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;initial_vertex_NList error: graph must not be nullptr!&quot;</span>);</div>
<div class="line"><a name="l00159"></a><span class="lineno">  159</span>&#160;            <span class="keywordflow">if</span>(src&lt;0||src&gt;=GraphType::NUM||dst&lt;0||dst&gt;=GraphType::NUM)</div>
<div class="line"><a name="l00160"></a><span class="lineno">  160</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;initial_vertex_NList error:id must &gt;=0 and &lt;N.&quot;</span>);</div>
<div class="line"><a name="l00161"></a><span class="lineno">  161</span>&#160;            <span class="keywordflow">if</span>(!graph-&gt;vertexes.at(src) || !graph-&gt;vertexes.at(dst))</div>
<div class="line"><a name="l00162"></a><span class="lineno">  162</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;initial_vertex_NList error: vertex of id does not exist.&quot;</span>);</div>
<div class="line"><a name="l00163"></a><span class="lineno">  163</span>&#160;            <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i&lt;GraphType::NUM;i++)</div>
<div class="line"><a name="l00164"></a><span class="lineno">  164</span>&#160;            {</div>
<div class="line"><a name="l00165"></a><span class="lineno">  165</span>&#160;                <span class="keywordflow">if</span>(i==src||i==dst) <span class="keywordflow">continue</span>;</div>
<div class="line"><a name="l00166"></a><span class="lineno">  166</span>&#160;                <span class="keyword">auto</span> vertex_u=graph-&gt;vertexes[i];</div>
<div class="line"><a name="l00167"></a><span class="lineno">  167</span>&#160;                <span class="comment">//************ 扫描邻接矩阵  **************</span></div>
<div class="line"><a name="l00168"></a><span class="lineno">  168</span>&#160;                <span class="keyword">const</span> <span class="keyword">auto</span>&amp; matrix=graph-&gt;matrix.matrix;</div>
<div class="line"><a name="l00169"></a><span class="lineno">  169</span>&#160;                <span class="keyword">const</span> <span class="keyword">auto</span> &amp; invalid_weight=graph-&gt;matrix.invalid_weight;</div>
<div class="line"><a name="l00170"></a><span class="lineno">  170</span>&#160;                <span class="keywordflow">for</span>(<span class="keywordtype">int</span> j=0;j&lt;GraphType::NUM;j++)</div>
<div class="line"><a name="l00171"></a><span class="lineno">  171</span>&#160;                {</div>
<div class="line"><a name="l00172"></a><span class="lineno">  172</span>&#160;                    <span class="keywordflow">if</span>(matrix[i][j]!=invalid_weight)<span class="comment">//从u出发的边</span></div>
<div class="line"><a name="l00173"></a><span class="lineno">  173</span>&#160;                    {</div>
<div class="line"><a name="l00174"></a><span class="lineno">  174</span>&#160;                        std::shared_ptr&lt;VertexNListNodeType&gt; node=std::make_shared&lt;VertexNListNodeType&gt;();</div>
<div class="line"><a name="l00175"></a><span class="lineno">  175</span>&#160;                        node-&gt;value=graph-&gt;vertexes[j];</div>
<div class="line"><a name="l00176"></a><span class="lineno">  176</span>&#160;                        vertex_u-&gt;N_List.add(node);</div>
<div class="line"><a name="l00177"></a><span class="lineno">  177</span>&#160;                    }</div>
<div class="line"><a name="l00178"></a><span class="lineno">  178</span>&#160;                    <span class="keywordflow">if</span>(matrix[j][i]!=invalid_weight)<span class="comment">//进入u的边</span></div>
<div class="line"><a name="l00179"></a><span class="lineno">  179</span>&#160;                    {</div>
<div class="line"><a name="l00180"></a><span class="lineno">  180</span>&#160;                        std::shared_ptr&lt;VertexNListNodeType&gt; node=std::make_shared&lt;VertexNListNodeType&gt;();</div>
<div class="line"><a name="l00181"></a><span class="lineno">  181</span>&#160;                        node-&gt;value=graph-&gt;vertexes[j];</div>
<div class="line"><a name="l00182"></a><span class="lineno">  182</span>&#160;                        vertex_u-&gt;N_List.add(node);</div>
<div class="line"><a name="l00183"></a><span class="lineno">  183</span>&#160;                    }</div>
<div class="line"><a name="l00184"></a><span class="lineno">  184</span>&#160;                }</div>
<div class="line"><a name="l00185"></a><span class="lineno">  185</span>&#160;                vertex_u-&gt;N_List.current=vertex_u-&gt;N_List.head;<span class="comment">//将 u.N.current设为u.N.head</span></div>
<div class="line"><a name="l00186"></a><span class="lineno">  186</span>&#160;            }</div>
<div class="line"><a name="l00187"></a><span class="lineno">  187</span>&#160;        }</div>
<div class="line"><a name="l00188"></a><span class="lineno">  188</span>&#160;</div>
<div class="line"><a name="l00190"></a><span class="lineno">  190</span>&#160;</div>
<div class="line"><a name="l00279"></a><span class="lineno">  279</span>&#160;        <span class="keyword">template</span>&lt;<span class="keyword">typename</span> GraphType&gt; std::array&lt;std::array&lt;typename GraphType::EWeightType,GraphType::NUM&gt;,GraphType::NUM&gt;</div>
<div class="line"><a name="l00280"></a><span class="lineno"><a class="line" href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#abafb73bda29c4e389edb048a5e5d8d2b">  280</a></span>&#160;        <a class="code" href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#abafb73bda29c4e389edb048a5e5d8d2b">relabel_to_front</a>(std::shared_ptr&lt;GraphType&gt; graph,<span class="keyword">typename</span> GraphType::VIDType src,<span class="keyword">typename</span> GraphType::VIDType dst)</div>
<div class="line"><a name="l00281"></a><span class="lineno">  281</span>&#160;        {</div>
<div class="line"><a name="l00282"></a><span class="lineno">  282</span>&#160;               <span class="keyword">typedef</span> <span class="keyword">typename</span> GraphType::EWeightType EWeightType;</div>
<div class="line"><a name="l00283"></a><span class="lineno">  283</span>&#160;               <span class="keyword">typedef</span> std::array&lt;std::array&lt;EWeightType,GraphType::NUM&gt;,GraphType::NUM&gt; MatrixType;</div>
<div class="line"><a name="l00284"></a><span class="lineno">  284</span>&#160;</div>
<div class="line"><a name="l00285"></a><span class="lineno">  285</span>&#160;</div>
<div class="line"><a name="l00286"></a><span class="lineno">  286</span>&#160;</div>
<div class="line"><a name="l00287"></a><span class="lineno">  287</span>&#160;                <span class="keywordflow">if</span>(!graph)</div>
<div class="line"><a name="l00288"></a><span class="lineno">  288</span>&#160;                    <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;relabel_to_front error: graph must not be nullptr!&quot;</span>);</div>
<div class="line"><a name="l00289"></a><span class="lineno">  289</span>&#160;                <span class="keywordflow">if</span>(src&lt;0||src&gt;=GraphType::NUM||dst&lt;0||dst&gt;=GraphType::NUM)</div>
<div class="line"><a name="l00290"></a><span class="lineno">  290</span>&#160;                    <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;relabel_to_front error:id must &gt;=0 and &lt;N.&quot;</span>);</div>
<div class="line"><a name="l00291"></a><span class="lineno">  291</span>&#160;                <span class="keywordflow">if</span>(!graph-&gt;vertexes.at(src) || !graph-&gt;vertexes.at(dst))</div>
<div class="line"><a name="l00292"></a><span class="lineno">  292</span>&#160;                    <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;relabel_to_front error: vertex of id does not exist.&quot;</span>);</div>
<div class="line"><a name="l00293"></a><span class="lineno">  293</span>&#160;</div>
<div class="line"><a name="l00294"></a><span class="lineno">  294</span>&#160;               MatrixType flow;</div>
<div class="line"><a name="l00295"></a><span class="lineno">  295</span>&#160;               <a class="code" href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#aeaf3e258f5ae9aed76158255fa603c00">initialize_preflow</a>(graph,src,flow);</div>
<div class="line"><a name="l00296"></a><span class="lineno">  296</span>&#160;</div>
<div class="line"><a name="l00297"></a><span class="lineno">  297</span>&#160;               <span class="keyword">auto</span> L=<a class="code" href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#ad6a1917551c87991625d8402593ec863">create_L</a>(graph,src,dst); <span class="comment">// create List L</span></div>
<div class="line"><a name="l00298"></a><span class="lineno">  298</span>&#160;               <a class="code" href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#af1dfc9c1874850fb1415db3644497777">initial_vertex_NList</a>(graph,src,dst); <span class="comment">//create u.N for each u</span></div>
<div class="line"><a name="l00299"></a><span class="lineno">  299</span>&#160;</div>
<div class="line"><a name="l00300"></a><span class="lineno">  300</span>&#160;               <span class="comment">//************   循环 **************</span></div>
<div class="line"><a name="l00301"></a><span class="lineno">  301</span>&#160;</div>
<div class="line"><a name="l00302"></a><span class="lineno">  302</span>&#160;               <span class="keyword">auto</span> node_u=L.head;</div>
<div class="line"><a name="l00303"></a><span class="lineno">  303</span>&#160;               <span class="keywordflow">while</span>(node_u)</div>
<div class="line"><a name="l00304"></a><span class="lineno">  304</span>&#160;               {</div>
<div class="line"><a name="l00305"></a><span class="lineno">  305</span>&#160;                   <span class="keyword">auto</span> vertex_u=node_u-&gt;value.lock();</div>
<div class="line"><a name="l00306"></a><span class="lineno">  306</span>&#160;                   assert(vertex_u);</div>
<div class="line"><a name="l00307"></a><span class="lineno">  307</span>&#160;                   <span class="keyword">auto</span> old_height=vertex_u-&gt;h;   <span class="comment">//保存旧h值</span></div>
<div class="line"><a name="l00308"></a><span class="lineno">  308</span>&#160;                   <a class="code" href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#a2ae42c12c93664d94b5a6f9980fe8540">discharge</a>(graph,vertex_u-&gt;id,flow); <span class="comment">//释放u</span></div>
<div class="line"><a name="l00309"></a><span class="lineno">  309</span>&#160;                   <span class="keywordflow">if</span>(vertex_u-&gt;h&gt;old_height)        <span class="comment">//若重贴标签则h值增加，则u前置到L头部</span></div>
<div class="line"><a name="l00310"></a><span class="lineno">  310</span>&#160;                   {</div>
<div class="line"><a name="l00311"></a><span class="lineno">  311</span>&#160;                        <span class="keywordflow">if</span>(node_u!=L.head) <span class="comment">//当u已经是L头时无需操作</span></div>
<div class="line"><a name="l00312"></a><span class="lineno">  312</span>&#160;                        {</div>
<div class="line"><a name="l00313"></a><span class="lineno">  313</span>&#160;                            L.front_of(node_u)-&gt;next=node_u-&gt;next;</div>
<div class="line"><a name="l00314"></a><span class="lineno">  314</span>&#160;                            node_u-&gt;next=L.head;</div>
<div class="line"><a name="l00315"></a><span class="lineno">  315</span>&#160;                            L.head=node_u;</div>
<div class="line"><a name="l00316"></a><span class="lineno">  316</span>&#160;                        }</div>
<div class="line"><a name="l00317"></a><span class="lineno">  317</span>&#160;                   }</div>
<div class="line"><a name="l00318"></a><span class="lineno">  318</span>&#160;                   node_u=node_u-&gt;next;</div>
<div class="line"><a name="l00319"></a><span class="lineno">  319</span>&#160;               }</div>
<div class="line"><a name="l00320"></a><span class="lineno">  320</span>&#160;               <span class="keywordflow">return</span> flow;</div>
<div class="line"><a name="l00321"></a><span class="lineno">  321</span>&#160;        }</div>
<div class="line"><a name="l00322"></a><span class="lineno">  322</span>&#160;    }</div>
<div class="line"><a name="l00323"></a><span class="lineno">  323</span>&#160;}</div>
<div class="line"><a name="l00324"></a><span class="lineno">  324</span>&#160;<span class="preprocessor">#endif // RELABELTOFRONT</span></div>
<div class="line"><a name="l00325"></a><span class="lineno">  325</span>&#160;</div>
<div class="ttc" id="namespace_introduction_to_algorithm_1_1_graph_algorithm_html_abafb73bda29c4e389edb048a5e5d8d2b"><div class="ttname"><a href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#abafb73bda29c4e389edb048a5e5d8d2b">IntroductionToAlgorithm::GraphAlgorithm::relabel_to_front</a></div><div class="ttdeci">std::array&lt; std::array&lt; typename GraphType::EWeightType, GraphType::NUM &gt;, GraphType::NUM &gt; relabel_to_front(std::shared_ptr&lt; GraphType &gt; graph, typename GraphType::VIDType src, typename GraphType::VIDType dst)</div><div class="ttdoc">relabel_to_front：最大流的前置重贴标签算法。算法导论26章26.5节 </div><div class="ttdef"><b>Definition:</b> <a href="relabeltofront_8h_source.html#l00280">relabeltofront.h:280</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_1_1_graph_algorithm_html_a143924362cc9f23ca452c3ca0e292ff3"><div class="ttname"><a href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#a143924362cc9f23ca452c3ca0e292ff3">IntroductionToAlgorithm::GraphAlgorithm::relabel</a></div><div class="ttdeci">void relabel(std::shared_ptr&lt; GraphType &gt; graph, typename GraphType::VIDType u_id, const std::array&lt; std::array&lt; typename GraphType::EWeightType, GraphType::NUM &gt;, GraphType::NUM &gt; &amp;flow)</div><div class="ttdoc">relabel：generic_push_relabel算法的relabel操作。算法导论26章26.4节 </div><div class="ttdef"><b>Definition:</b> <a href="genericpushrelabel_8h_source.html#l00184">genericpushrelabel.h:184</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_list_html"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_list.html">IntroductionToAlgorithm::GraphAlgorithm::List</a></div><div class="ttdoc">List：链表数据结构 </div><div class="ttdef"><b>Definition:</b> <a href="front__flow__vertex_8h_source.html#l00037">front_flow_vertex.h:37</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_1_1_graph_algorithm_html_a2ae42c12c93664d94b5a6f9980fe8540"><div class="ttname"><a href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#a2ae42c12c93664d94b5a6f9980fe8540">IntroductionToAlgorithm::GraphAlgorithm::discharge</a></div><div class="ttdeci">void discharge(std::shared_ptr&lt; GraphType &gt; graph, typename GraphType::VIDType u_id, std::array&lt; std::array&lt; typename GraphType::EWeightType, GraphType::NUM &gt;, GraphType::NUM &gt; &amp;flow)</div><div class="ttdoc">discharge：最大流的前置重贴标签算法中的释放操作。算法导论26章26.5节 </div><div class="ttdef"><b>Definition:</b> <a href="relabeltofront_8h_source.html#l00053">relabeltofront.h:53</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_1_1_graph_algorithm_html_abd520b7e33d8c1e72b05210d784ac258"><div class="ttname"><a href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#abd520b7e33d8c1e72b05210d784ac258">IntroductionToAlgorithm::GraphAlgorithm::push</a></div><div class="ttdeci">void push(std::shared_ptr&lt; GraphType &gt; graph, typename GraphType::VIDType u_id, typename GraphType::VIDType v_id, std::array&lt; std::array&lt; typename GraphType::EWeightType, GraphType::NUM &gt;, GraphType::NUM &gt; &amp;flow)</div><div class="ttdoc">push：generic_push_relabel算法的push操作。算法导论26章26.4节 </div><div class="ttdef"><b>Definition:</b> <a href="genericpushrelabel_8h_source.html#l00067">genericpushrelabel.h:67</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_1_1_graph_algorithm_html_aeaf3e258f5ae9aed76158255fa603c00"><div class="ttname"><a href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#aeaf3e258f5ae9aed76158255fa603c00">IntroductionToAlgorithm::GraphAlgorithm::initialize_preflow</a></div><div class="ttdeci">void initialize_preflow(std::shared_ptr&lt; GraphType &gt; graph, typename GraphType::VIDType src, std::array&lt; std::array&lt; typename GraphType::EWeightType, GraphType::NUM &gt;, GraphType::NUM &gt; &amp;flow)</div><div class="ttdoc">initialize_preflow：generic_push_relabel算法的初始化操作。算法导论26章26.4节 </div><div class="ttdef"><b>Definition:</b> <a href="genericpushrelabel_8h_source.html#l00227">genericpushrelabel.h:227</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_1_1_graph_algorithm_html_ad6a1917551c87991625d8402593ec863"><div class="ttname"><a href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#ad6a1917551c87991625d8402593ec863">IntroductionToAlgorithm::GraphAlgorithm::create_L</a></div><div class="ttdeci">List&lt; ListNode&lt; typename GraphType::VertexType &gt; &gt; create_L(std::shared_ptr&lt; GraphType &gt; graph, typename GraphType::VIDType src, typename GraphType::VIDType dst)</div><div class="ttdoc">create_L：前置重贴标签算法中的创建L链表操作 </div><div class="ttdef"><b>Definition:</b> <a href="relabeltofront_8h_source.html#l00113">relabeltofront.h:113</a></div></div>
<div class="ttc" id="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_list_node_html"><div class="ttname"><a href="struct_introduction_to_algorithm_1_1_graph_algorithm_1_1_list_node.html">IntroductionToAlgorithm::GraphAlgorithm::ListNode</a></div><div class="ttdoc">ListNode：链表结点的数据结构 </div><div class="ttdef"><b>Definition:</b> <a href="front__flow__vertex_8h_source.html#l00133">front_flow_vertex.h:133</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_html"><div class="ttname"><a href="namespace_introduction_to_algorithm.html">IntroductionToAlgorithm</a></div><div class="ttdoc">Namespace of IntrodunctionToAlgorithm. </div><div class="ttdef"><b>Definition:</b> <a href="longest__common__subsequence_8h_source.html#l00024">longest_common_subsequence.h:24</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_1_1_graph_algorithm_html_af1dfc9c1874850fb1415db3644497777"><div class="ttname"><a href="namespace_introduction_to_algorithm_1_1_graph_algorithm.html#af1dfc9c1874850fb1415db3644497777">IntroductionToAlgorithm::GraphAlgorithm::initial_vertex_NList</a></div><div class="ttdeci">void initial_vertex_NList(std::shared_ptr&lt; GraphType &gt; graph, typename GraphType::VIDType src, typename GraphType::VIDType dst)</div><div class="ttdoc">initial_vertex_NList：前置重贴标签算法中的初始化邻接链表操作 </div><div class="ttdef"><b>Definition:</b> <a href="relabeltofront_8h_source.html#l00153">relabeltofront.h:153</a></div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_8ed9dba4cd616b67da9b1338594e34e0.html">src</a></li><li class="navelem"><a class="el" href="dir_aa0a5ddea82682e3e6fbdbaa3337ee0e.html">graph_algorithms</a></li><li class="navelem"><a class="el" href="dir_a4fbac2a12224e4d8b34a92a8974f6d5.html">max_flow</a></li><li class="navelem"><a class="el" href="dir_eb8c39b198121492c4c77898df27a1f1.html">relabel_to_front</a></li><li class="navelem"><a class="el" href="relabeltofront_8h.html">relabeltofront.h</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.10 </li>
  </ul>
</div>
</body>
</html>
